package cn.simon.controller;

import cn.simon.model.Notice;
import cn.simon.service.NoticeService;
import lombok.extern.java.Log;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.Date;
import java.util.List;

@Log
@Controller
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;


    /**
     * 公告列表
     * @param model
     * @return
     */
    @GetMapping("list")
    public String noticeList(Model model){
        List<Notice> list = noticeService.noticeList();
        model.addAttribute("list",list);
        return "noticeList";
    }
    /**
     * 发布公告
     * @return
     */
    @GetMapping("/add")
    public String createNotice(){
        return "addNotice";
    }
    @PostMapping("/add")
    public String createNotice(Notice notice,Model model){
        Date date = new Date();
        notice.setNoticeCreateTime(date);
        model.addAttribute("notice",notice);
        noticeService.createNotice(notice);
        return "redirect:/notice/list";
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @GetMapping("delete")
    public String deleteNotice(Integer id){
        noticeService.deleteById(id);
        return "redirect:/notice/list";
    }

    /**
     * 修改公告(发布时间不可变)
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/update")
    public String updateNotice(Integer id,Model model){
        Notice notice = noticeService.findById(id);
        model.addAttribute("notice",notice);
        return "updateNotice";
    }
    @PostMapping("/update")
    public String updateNotice(Notice notice){
        noticeService.updateById(notice);
        return "redirect:/notice/list";
    }
}
